<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getEmployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工名字" width="120" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="180" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="180" sortable>
			</el-table-column>
			<el-table-column prop="salt" label="盐值" width="180" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="180" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" width="100" sortable>
				<template scope="scope">
					<span style="color: #5daf34" v-if="scope.row.state === 1">在职</span>
					<span style="color:red" v-else-if="scope.row.state === 0">离职</span>
					<span style="color:dimgrey" v-else>休假</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="titel" :visible.sync="employeeFormVisible" :close-on-click-modal="false">
			<el-form :model="employeeForm" label-width="80px" :rules="employeeFormRules" ref="employeeForm">
				<el-form-item label="员工名字" prop="username">
					<el-input v-model="employeeForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="employeeForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话" prop="phone">
					<el-input v-model="employeeForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="盐值" prop="salt">
					<el-input v-model="employeeForm.salt" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="employeeForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="employeeForm.state">
						<el-radio class="radio" :label="1">在职</el-radio>
						<el-radio class="radio" :label="0">离职</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="employeeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="employeeSubmit" :loading="employeeLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
    import util from '../../common/js/util'
    //import NProgress from 'nprogress'
    import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, employeeDepartment, addDepartment } from '../../api/api';

    export default {
        data() {
            return {
                title: '',
                filters: {
                    keyword: ''
                },
                employees: [],
                total: 0,
                pageSize:5,
                page: 1,
                listLoading: false,
                sels: [],//列表选中行

                employeeFormVisible: false,//编辑界面是否显示
                employeeLoading: false,
                employeeFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                employeeForm: {
                    id: null,
                    username: '',
					email:'',
                    phone: '',
                    salt: '',
                    age: null,
                    state: '1'
                },
            }
        },
        methods: {
            /*//性别显示转换
            formatSex: function (row, column) {
                return row.state == 0 ? '<span style="color:green;>"启用</span>' : row.state == -1 ? '禁用' : '未知';
            },*/
            handleCurrentChange(val) {
                this.page = val;
                this.getEmployees();
            },
            //获取部门列表
            getEmployees() {
                let para = {
                    currentPage: this.page,
                    keyword: this.filters.keyword,
                    pageSize:this.pageSize
                };
                this.listLoading = true;//开启忙等框
                this.$http.post("/employee",para)
                    .then(result=>{
                        result = result.data;//PageList{total rows}
                        this.employees = result.rows;
                        this.listLoading = false;//关闭忙等框
                        this.total = result.total;
                    })
                    .catch(result=>{
                        console.log(result);
                        alert("系统错误！");
                    })
                /*getDepartmentListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.employees = res.data.employees;
                    this.listLoading = false;
                    //NProgress.done();
                });*/
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {//点击确定
                    this.listLoading = true;//开启忙等框
                    //NProgress.start();
                    this.$http.delete("/employee/"+ row.id)
                        .then(result=>{
                            //1.关闭忙等
                            this.listLoading = false;
                            this.employeeFormVisible = false;//关闭弹出框
                            //判断业务操作是否正常
                            result  = result.data;//AjaxResult  {success:false,message:操作失败}
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示我们自己的业务错
                                    type: 'error'
                                });
                            }
                            //2.刷新页面
                            //是否需要重新刷新页面   要  定位到第一页
                            this.page = 1;
                            this.getEmployees();
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            this.employeeFormVisible = false;//关闭弹出框
                            //NProgress.done();
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            //失败了是否需要重新刷新页面   要  定位到第一页
                            this.page = 1;
                            this.getEmployees();
                        })
                }).catch(() => {//点击取消

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.titel = "编辑";
                this.employeeFormVisible = true;
                //Object.assign 克隆
                this.employeeForm = Object.assign({}, row);//回显
            },
            //显示新增界面
            handleAdd: function () {
                this.titel = "新增";
                this.employeeFormVisible = true;
                this.employeeForm = {//清空历史遗留数据
                    username: '',
                    email:'',
                    phone: '',
                    salt: '',
                    age: null,
                    state: '1'
                };
            },
            //编辑
            employeeSubmit: function () {
                this.$refs.employeeForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.employeeLoading = true; // 开启忙等框
                            //NProgress.start();
                            let para = Object.assign({}, this.employeeForm);// 克隆一份表数据
                            this.$http.put("/employee",para)
                                .then(result=>{
                                    this.employeeLoading = false;
                                    this.employeeFormVisible = false;//关闭弹出框
                                    result = result.data;
                                    if(result.success){
                                        this.$message({
                                            message:"操作成功",
                                            type:"success"
                                        })
                                    }else{
                                        this.$message({
                                            message:result.message,// 后台代码异常
                                            type:"error"
                                        })
                                    }
                                    // 刷新页面到第一页
                                    this.page = 1;
                                    this.getEmployees();
                                }).catch(result=>{
                                this.employeeLoading = false;
                                this.employeeFormVisible = false;//关闭弹出框
                                this.$message({
                                    message:"操作失败",
                                    type:"error"
                                });
                                // 刷新页面到第一页
                                this.page = 1;
                                this.getEmployees();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {//当前已经选中的所有行的数据
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);//[1,2,3]
                this.$confirm('确认删除选中记录吗？', '温馨提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    this.$http.patch("/employee",ids)
                        .then(result=>{
                            //1.关闭忙等
                            this.listLoading = false;
                            //判断业务操作是否正常
                            result  = result.data;//AjaxResult  {success:false,message:操作失败}
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示我们自己的业务错
                                    type: 'error'
                                });
                            }
                            //2.刷新页面
                            //是否需要重新刷新页面   要  定位到第一页
                            this.page = 1;
                            this.getEmployees();
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            //失败了是否需要重新刷新页面   要  定位到第一页
                            this.page = 1;
                            this.getEmployees();
                        });

                    /*batchRemoveDepartment(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getEmployees();
                    });*/
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getEmployees();
        }
    }

</script>

<style scoped>


</style>